const app = getApp()
import {
  imageUrl
} from "../../utils/imageutil.js"
let my_this = null;
Page({
  data: {
    imageUrl,
    list: [{
        title: '订单编号',
        text: '',
        by: '1'
      },
      {
        title: '创建时间',
        text: ''
      },
      {
        title: '付款时间',
        text: ''
      },
      // {title:'发货时间',text:''},
      // {title:'邮寄方式',text:'京东快递'},
      // {title:'快递单号',text:'28272629361858889',by:'1'},
    ],
    site: { //地址信息
      address: '',
      doorplate: '',
      user_name: '',
      tel: ''
    },
    id: '',
    img: '',
    good_name: '',
    discount_money: '',
    coupon_money: '',
    actual_integral: '',
    integral: '',
    orderDetail: '',
    esm_list: [],
    express_price: 0,
    is_express_price: 0,
    express_num: 0,
    num: 0,
    happiness_integral: 0,
    coupon_money: 0,
    type_id: 0,
    ordertype: 0, //0 单商品 1多商品
    do: '',
    wallet: 0,
  },
  onLoad(options) {
    wx.removeStorageSync('id');
    if (options.id) {
      this.setData({
        id: options.id,
        ordertype: options.ordertype,
      })
      if (options.ordertype == 0) {
        this.setData({
          do: 'JfOrderDetail'
        })
      } else if (options.ordertype == 1) {
        this.setData({
          do: 'JfOrderDetail2'
        })
      }

    }
  },
  onShow() {
    my_this = this;
    this.MyAddressInfo()
    this.getList().then(res => {
      if (res.ems_code) {
        this.queryExpress()
      } else if (res.third_order_sn) {
        // this.gylwl()
        this.wb_logistics()
      }
    })
  },
  openTelephone() { //拨打客服电话
    wx.showActionSheet({
      itemList: ['线上客服'],
      success(res) {
        if (res.tapIndex == 0) {
          getApp().contact(5)
          // getApp().showModal('15875377469',true,'确定','取消','是否拨打客服电话').then(()=>{
          //   wx.makePhoneCall({
          //     phoneNumber: '15875377469'
          //   })
          // })
        } else if (res.tapIndex == 1) {
          getApp().contact()
        }
      }
    })
  },
  queryExpress() {
    wx.request({
      url: getApp().globalData.ht_api + '/api/index/ExpressDelivery',
      data: {
        num: this.data.orderDetail.EMS,
        com: this.data.orderDetail.ems_code
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'GET',
      success: (res) => {
        this.setData({
          esm_list: res.data
        })
      },
    })
  },
  gylwl() {
    getApp().globalData.posts('/api/ouyoung/order/sorting/ouyapi/gylwl', {
      order_id: this.data.list[0].text
    }).then(res => {
      if (res.data.code == 1) {
        this.setData({
          esm_list: res.data.data
        })
      }
    })
  },
  wb_logistics() {
    getApp().globalData.posts('/index.php/api/ouyoung/chat/sorting/ouyapi/wb_logistics', {
      order_id: this.data.orderDetail.third_order_sn
    }).then(res => {
      if (res.data.code == 1) {
        let esm_list = res.data.data;
        esm_list.forEach((v, i) => {
          if (v.content) {
            v.context = v.content
          }
          if (v.msgTime) {
            v.time = v.msgTime
          }
        })
        this.setData({
          esm_list
        })
      }
    })
  },
  copyText: function (e) { //复制单号
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success(res) {
        wx.showToast({
          title: '复制成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  to_site() { //选择地址
    wx.navigateTo({
      url: '../matron/address?type=1',
    })
  },
  MyAddressInfo() { //获取地址
    getApp().globalData.post({
      data: {
        do: "MyAddressInfo",
        id: wx.getStorageSync('id')
      }
    }).then(res => {
      console.log(res)
      this.setData({
        ['site.user_name']: res.data.user_name,
        ['site.tel']: res.data.tel,
        ['site.address']: res.data.address,
        ['site.doorplate']: res.data.doorplate
      })
    })
  },
  getList() {
    return new Promise((reslove, reject) => {
      app.globalData.post({
        data: {
          do: this.data.do,
          id: this.data.id
        }
      }).then(res => {
        let list = res.data.result
        wx.nextTick(() => {
          my_this.setData({
            orderDetail: list,
            ['site.user_name']: list.user_name,
            ['site.tel']: list.user_tel,
            ['site.address']: list.address,
            img: list.good_img,
            good_name: list.good_name,
            discount_money: list.discount_money,
            coupon_money: list.coupon_money,
            actual_integral: list.actual_integral,
            integral: list.integral,
            express_price: Number(list.express_price),
            is_express_price: Number(list.is_express_price),
            express_num: Number(list.express_num),
            num: Number(list.num),
            type_id: list.type_id,
            happiness_integral: Number(list.happiness_integral_true),
            coupon_money: Number(list.coupon_money),
            wallet: Number(list.wallet),
            'list[0].text': list.order_id,
            'list[1].text': list.time,
            'list[2].text': list.pay_time,
          })
          if (list.ems_code || list.third_order_sn) {
            reslove({
              ems_code: list.ems_code,
              third_order_sn: list.third_order_sn
            })
          }
        })
      })
    })
  },
  to_Cancel(e) { //取消订单
    wx.navigateTo({
      url: '../matron/abrogate?wid=' + e.currentTarget.dataset.id + '&type=' + 3,
    })
  },
  applyRefundJfOrder(e) {
    if (e.currentTarget.dataset.refund > 0) {
      return
    }
    wx.navigateTo({
      url: '/pages/order/returnGoods?order=' + e.currentTarget.dataset.order,
    })
  },
  pay(e) {
    if (e.currentTarget.dataset.id.indexOf(",") != -1) {
      wx.navigateTo({
        url: '/store/submit_more?ids=' + e.currentTarget.dataset.cartid + '&orderid=' + e.currentTarget.dataset.orderid,
      })
    } else {
      if (this.data.orderDetail.num >= 2 && e.currentTarget.dataset.cartid) {
        wx.navigateTo({
          url: '/store/submit_more?ids=' + e.currentTarget.dataset.cartid + '&orderid=' + e.currentTarget.dataset.orderid,
        })
      } else {
        if (e.currentTarget.dataset.ggs) {
          wx.navigateTo({
            url: '/store/submit?id=' + e.currentTarget.dataset.id + '&ggs=' + e.currentTarget.dataset.ggs + '&orderid=' + e.currentTarget.dataset.orderid,
          })
        } else {
          wx.navigateTo({
            url: '/store/submit?id=' + e.currentTarget.dataset.id + '&orderid=' + e.currentTarget.dataset.orderid,
          })
        }
      }
    }
  },
  jfcheck(e) { //确认收货
    getApp().globalData.post({
      data: {
        do: 'jfcheck',
        jforder_id: e.currentTarget.dataset.id,
        user_id: wx.getStorageSync('userInfo').id
      }
    }).then(res => {
      getApp().showToast(res.data.msg)
      if (res.data.code == 1) {
        this.getList()
      }
    })
  },
  appraise(e) {
    wx.navigateTo({
      url: './mall_appraise?id=' + e.currentTarget.dataset.id + '&ordertype=' + e.currentTarget.dataset.ordertype,
    })
  },
  catch_tap() {},
})